/*
package com.skyMailbox.sms.service.impl;

import com.skyMailbox.sms.service.helper.ProcesadorMensajes;
import com.sun.comm.Win32Driver;

import javax.comm.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;


public class PuertoSerieManager implements Runnable, SerialPortEventListener {

    static Enumeration portList;
    static CommPortIdentifier portId;
    final String CRLF = "\r\n";
    String incomeMessage = CRLF + "+CMTI:";
    String getMessage = "AT+CMGR=";
    String deleteMessage = "AT+CMGD=";
    String retrivedMessage = CRLF + "+CMGR";
    //static String messageString = "AT+CMGS=\"669593396\"" + (char) 13 + "Este mensaje es de prueba mandado desde la aplicacion Java" +(char) 26;
    static String messageString = "AT+CMGL=\"ALL\"" + (char) 13;
    static SerialPort serialPort;
    private boolean open;
    static OutputStream outputStream;
    static InputStream inputStream;
    Thread readThread;
    Thread readThread2;
    static boolean outputBufferEmptyFlag = false;

    static CommDriver commdriver;

    public PuertoSerieManager() throws Exception {
        if (commdriver == null) {
            boolean portFound = false;

            String defaultPort = "COM4";

            initializeDriver();

            portList = CommPortIdentifier.getPortIdentifiers();

            while (portList.hasMoreElements()) {

                portId = (CommPortIdentifier) portList.nextElement();

                if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

                    if (portId.getName().equals(defaultPort)) {

                        System.out.println("Se encontro el puerto " + defaultPort + " !!!");

                        portFound = true;

                        openConnection();
                    }
                }
            }

            if (!portFound) {
                System.out.println("Puerto " + defaultPort + ", no se encontro :(");
            }
        }
    }


    private static void initializeDriver() {
        */
/*try {

            commdriver = new RXTXCommDriver();
            commdriver.initialize();
        }
        catch (UnsatisfiedLinkError er) {
            System.out.println("Error al inizialiar el driver = " + er);
        }
        catch (Exception e2) {
            e2.printStackTrace();
        }*//*


        String driverName = "com.sun.comm.Win32Driver";
        try {
            Win32Driver win32Driver = (Win32Driver) Class.forName(driverName).newInstance();
            CommDriver commdriver = (CommDriver) win32Driver;
            commdriver.initialize();
        }
        catch (Exception e2) {
            e2.printStackTrace();
        }
    }

    public static void setConnectionParameters() throws Exception {

        // Set connection parameters, if set fails return parameters object
        // to original state.
        try {
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
            throw new Exception("Parametros no soportados");
        }
        // Set flow control.
        try {
            serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
        } catch (UnsupportedCommOperationException e) {
            throw new Exception("Control de flujo no soportado");
        }
    }

    public void serialEvent(SerialPortEvent e) {


        long time = System.currentTimeMillis();
        // Create a StringBuffer and int to receive input data.
        StringBuffer inputBuffer = new StringBuffer();
        int newData = 0;

        // Determine type of event.
        while (newData != -1) {
            try {
                newData = inputStream.read();
                if (newData == -1) {
                    break;
                }
                inputBuffer.append((char) newData);
            } catch (IOException ex) {
                System.err.println(ex);
                return;
            }
        }

        String mensaje = new String(inputBuffer);
        System.out.println("mensaje = " + mensaje);

        switch (e.getEventType()) {

            // Read data until -1 is returned. If \r is received substitute
            // \n for correct newline handling.
            case SerialPortEvent.DATA_AVAILABLE:
                System.out.println("");
                System.out.println("________________ START ____________");
                System.out.println("");


                while (newData != -1) {
                    try {
                        newData = inputStream.read();
                        if (newData == -1) {
                            break;
                        }
                        inputBuffer.append((char) newData);
                    } catch (IOException ex) {
                        System.err.println(ex);
                        return;
                    }
                }

                mensaje = new String(inputBuffer);
                System.out.println("mensaje = " + mensaje);

                try {
                    if (mensaje.startsWith(incomeMessage)) {
                        System.out.println("Llego un mensje en la posicion " + String.valueOf(mensaje.charAt(mensaje.indexOf(",") + 1)));
                        outputStream.write((getMessage + String.valueOf(mensaje.charAt(mensaje.indexOf(",") + 1)) + CRLF).getBytes());
                    } else if (mensaje.startsWith(retrivedMessage)) {
                        Thread thread = new Thread(new ProcesadorMensajes(mensaje));
                        System.out.println("llamo run");
                        thread.start();
                        System.out.println("sale run");
                        outputStream.write((deleteMessage + 1 + CRLF).getBytes());
                    }
                } catch (IOException es) {
                    System.out.println("e = " + es);
                }


                long time2 = System.currentTimeMillis() - time;
                System.out.println("");
                System.out.println("________________ END ____________" + time2 + " ms");


                break;

            // If break event append BREAK RECEIVED message.
            case SerialPortEvent.BI:
                System.out.println("--- BREAK RECEIVED ---");
        }


    }

    public void openConnection() throws Exception {

        try {
            //abro el puerto
            serialPort = (SerialPort) portId.open("SimpleWrite", 30000);

            // le coloco los parametros
            setConnectionParameters();

            // creo el outputStream de puerto serial
            outputStream = serialPort.getOutputStream();

            // creo el de l puerto serial
            inputStream = serialPort.getInputStream();


            serialPort.addEventListener(this);

            // se coloca a true notifyOnDataAvailable para manejador de eventos entre en accion
            serialPort.notifyOnDataAvailable(true);

            // se coloca a true notifyOnDataAvailable para que el manejador de eventos romapa la coneccion
            serialPort.notifyOnBreakInterrupt(true);

            serialPort.notifyOnDataAvailable(true);
            serialPort.notifyOnBreakInterrupt(true);
            serialPort.notifyOnCarrierDetect(true);
            serialPort.notifyOnCTS(true);
            serialPort.notifyOnDataAvailable(true);
            serialPort.notifyOnDSR(true);
            serialPort.notifyOnFramingError(true);
            serialPort.notifyOnOutputEmpty(true);
            serialPort.notifyOnOverrunError(true);
            serialPort.notifyOnParityError(true);
            serialPort.notifyOnRingIndicator(true);

            serialPort.enableReceiveTimeout(30);


            open = true;

        } catch (PortInUseException e) {
            System.out.println("Port in use.");
            serialPort.close();
        } catch (IOException e) {
            System.out.println("IOException outputStream.write " + e);
            serialPort.close();
        } catch (Exception e) {
            System.out.println("Exception.Thread  e" + e);
            System.out.println(e.toString());
            System.exit(-1);
            serialPort.close();
        }

    }

    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println("e = " + e);
        }

    }

    public void write(byte[] buffer)
            throws Exception {
        try {
            outputStream.write(buffer);
        } catch (IOException e) {
            throw new Exception("write error", e);
        }
    }
}





*/
